import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

public class Client {
    public static void main(String[] args) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        Socket socket = null;
        PrintWriter out = null;
        BufferedReader reader = null;

        String url = "jdbc:postgresql://localhost:5432/thingsboard";
        String username = "postgres";
        String password = "postgres";
        String sql = "SELECT * FROM ts_kv_latest WHERE key IN ('41','42')";

        try {
            // 注册JDBC驱动
            Class.forName("org.postgresql.Driver");

            // 打开连接
            conn = DriverManager.getConnection(url, username, password);

            // 准备SQL查询语句，将结果集设为可滚动
            ps = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);

            // 执行查询
            while (true) {
                rs = ps.executeQuery();

                // 获取元数据和查询结果
                ResultSetMetaData metadata = rs.getMetaData();
                int columnCount = metadata.getColumnCount();

                // 输出表头
                System.out.println("Table Name: ts_kv_latest\n");

                // 找到名为ldbl_v的列
                int dblVIndex = -1;
                for (int i = 1; i <= columnCount; i++) {
                    String columnName = metadata.getColumnName(i);
                    if (columnName.equals("dbl_v")) {
                        dblVIndex = i;
                        break;
                    }
                }

                if (dblVIndex == -1) {
                    System.out.println("No dbl_v column found");
                    return;
                }

                // 输出查询结果中ldbl_v列的值
                String resultStr = "";
                while (rs.next()) {
                    if (rs.getString("key").equals("41") || rs.getString("key").equals("42")) {
                        String dblV = rs.getString(dblVIndex);
                        resultStr += dblV + ",";
                    }
                }

                // 发送数据给服务器
                if (!resultStr.isEmpty()) {
                    // 去掉最后一个逗号
                    resultStr = resultStr.substring(0, resultStr.length() - 1);
                    socket = new Socket("10.22.214.4", 8888);
                    out = new PrintWriter(socket.getOutputStream(), true);
                    out.println(resultStr);

                    // 等待1秒
                    Thread.sleep(1000L);
                }

                // 获取服务端返回的数据，打印到控制台
                reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                String response = reader.readLine();
                System.out.println("Response from server: " + response);

                // 关闭套接字连接和输出流
                if (socket != null) {
                    socket.close();
                    out.close();
                }
            }

        } catch (ClassNotFoundException | SQLException | IOException | InterruptedException e) {
            e.printStackTrace();
        } finally {
            // 关闭资源
            try {
                if (rs != null) {
                    rs.close();
                }
                if (ps != null) {
                    ps.close();
                }
                if (conn != null) {
                    conn.close();
                }
                if (socket != null) {
                    socket.close();
                }
                if (out != null) {
                    out.close();
                }
                if (reader != null) {
                    reader.close();
                }
            } catch (SQLException | IOException e) {
                e.printStackTrace();
            }
        }
    }
}